<?php

namespace app\im\service;

use app\im\model\ChatRoomModel;

class ChatRoomService
{
    protected $db;

    public function __construct()
    {
        $this->db = new ChatRoomModel();
    }

    public function messages($roomId)
    {
        $room = $this->db->find($roomId);
        $messages = $room->messages()->limit(100)->select();

        return $messages;
    }

    public function sendMessage($roomId, $sendUserId, $msg)
    {
        $room = $this->db->find($roomId);
        $res = $room->messages()->save([
            'user_id' => $sendUserId,
            'content' => $msg,
            'create_time' => time(),
        ]);
        // TODO 发送websocket message
        return $res ? true : false;
    }
}
